package com.file_.BufferedReaderAndWriter_;

import java.io.*;

// 文件拷贝
public class BufferedCopy {
    public static void main(String[] args) {
        /*
         * 说明：
         * 1、BufferedReader和BufferedWriter是按 字符 来操作的
         * 2、所以不要去操作二进制文件，否则文件损坏（比如视频、图片、声音）
         * */

        String srcfilePath = "D:\\A_Test\\中英文.txt";
        String destfilePath = "D:\\A_Test\\图片\\中英文.txt";
        // 读出来，写进去
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        String line = null;

        try {
            // 创建读取写入对象
            bufferedReader = new BufferedReader(new FileReader(srcfilePath));
            bufferedWriter = new BufferedWriter(new FileWriter(destfilePath));

            while ((line = bufferedReader.readLine()) != null) {
                // 每读取一行，就写入一行
                bufferedWriter.write(line);
                // readLine()只是读取一行，不带换行符，所以必须换行
                bufferedWriter.newLine();
            }
            System.out.println("拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
